<?php

namespace App\Transformers\Company;

use App\Transformers\Encyclopedia\CategoryTransformer;
use App\Transformers\System\RegionTransformer;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\TransformerAbstract;

class MaterialTransformer extends TransformerAbstract
{

    protected $availableIncludes = ['region', 'category'];

    public function transform(Model $model)
    {
        return $model->attributesToArray();
    }

    public function includeRegion(Model $model)
    {
        $region = $model->region;
        if (!$region) {
            return $this->null();
        }
        return $this->item($region, new RegionTransformer());
    }

    public function includeCategory(Model $model)
    {
        $category = $model->category;
        if (!$category) {
            return $this->null();
        }
        return $this->item($category, new CategoryTransformer());
    }
}